package com.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.entity.FeeCar;
import com.community.entity.Parking;
import com.community.entity.ParkingInmanage;
import com.community.entity.ParkingOut;
import com.community.service.FeeCarService;
import com.community.service.ParkingInmanageService;
import com.community.service.ParkingOutService;
import com.community.service.ParkingService;
import com.community.util.Voice;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * (ParkingOut)表控制层
 *
 * @author makejava
 * @since 2021-11-09 22:33:03
 */
@Controller
@RequestMapping("parkingOut")
public class ParkingOutController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ParkingOutService parkingOutService;
    @Resource
    private ParkingInmanageService parkingInmanageService;
    @Resource
    ParkingService parkingService;
    @Resource
    private FeeCarService feeCarService;
    @Resource
    Voice voice;

    @RequestMapping("parking_out")
    public String parking_out() {
        return "parking_out";
    }


    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param parkingOut 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    @ResponseBody
    public R selectAll(Page<ParkingOut> page, ParkingOut parkingOut) {
        return success(this.parkingOutService.page(page, new QueryWrapper<>(parkingOut).orderByDesc("out_date")));
    }

    // 计算时间差
    String sub(Date in, Date out) {
        try {
            long diff = out.getTime() - in.getTime();//这样得到的差值是毫秒级别
            long days = diff / (1000 * 60 * 60 * 24);

            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            System.out.println("" + days + "天" + hours + "小时" + minutes + "分");
            return "" + days + "天" + hours + "小时" + minutes + "分";
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    /**
     * 根据车牌号查询最新该车辆信息
     *
     * @return 所有数据
     */
    @GetMapping("out")
    @ResponseBody
    public R selectByLicenseplate(String licenseplate) {
        //获取车辆收费信息
        double money = feeCarService.getOne(new QueryWrapper<FeeCar>().eq("fee_no", "922021120279280304")).getFeeMoney();
        HashMap hashMap = new HashMap();
        ParkingInmanage parkingInmanage = parkingInmanageService.selectByLicenseplate(licenseplate);
        if (parkingInmanage == null) return failed("场内未找到该车辆！");
//        voice.voice(licenseplate + "祝您一路顺风！");
        parkingInmanageService.removeById(parkingInmanage.getId());
        ParkingOut parkingOut = new ParkingOut();
        parkingOut.setCarNumber(licenseplate);
        parkingOut.setInDate(parkingInmanage.getInData());
        parkingOut.setOutDate(new Date());
        double diff = parkingOut.getOutDate().getTime() - parkingOut.getInDate().getTime();//这样得到的差值是微秒级别
        double hours = diff / (1000 * 60 * 60);
        double price = money * Math.round(hours);
        parkingOut.setMoney(parkingInmanage.getCarType().equals("固定车") ? 0 : price);
        parkingOutService.save(parkingOut);

        QueryWrapper<Parking> wrapper = new QueryWrapper<>();
        wrapper.eq("stall", parkingInmanage.getParking());
        Parking parking = new Parking();
        parking.setCarState(1);
        parkingService.update(parking, wrapper);

        hashMap.put("carNumber", licenseplate);
        hashMap.put("carType", parkingInmanage.getCarType());
        hashMap.put("inDate", parkingInmanage.getInData());
        hashMap.put("outDate", new Date());
        hashMap.put("onDate", parkingInmanage.getOnDate());
        hashMap.put("price", parkingInmanage.getCarType().equals("固定车") ? 0 : price);
        return success(hashMap);
    }

    /**
     * 新增数据
     *
     * @param parkingOut 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ResponseBody
    public R insert(ParkingOut parkingOut) {
        return success(this.parkingOutService.save(parkingOut));
    }

    /**
     * 修改数据
     *
     * @param parkingOut 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ResponseBody
    public R update(@RequestBody ParkingOut parkingOut) {
        return success(this.parkingOutService.updateById(parkingOut));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ResponseBody
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.parkingOutService.removeByIds(idList));
    }

    @GetMapping("sum")
    @ResponseBody
    public Object selectsum() {
        String sum = parkingOutService.priceSum();
        return sum == null ? 0 : sum;
    }
}

